from base.base import BaseTask
import requests
import json
import os
from base.notify import send
class ZkbgTask(BaseTask):

    def __init__(self, title, phone):
        self.userInfo = {
        }
        self.title = title
        self.userInfo['phone'] = phone
        return

    def login(self):
        headers = {
            'Host': 'www.biguotk.com',
            'device': '1',
            'channel': 'xiaomi',
            'devicemodel': '22041211AC',
            'deviceversion': '7.1.2',
            'appversion': '6.5.21',
            'ua': 'Mozilla/5.0 (Linux; Android 7.1.2; 22041211AC Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/81.0.4044.117 Mobile Safari/537.36',
            'oaid': '',
            'imei': '',
            'androidid': '',
            'content-type': 'application/x-www-form-urlencoded',
            'content-length': '209',
            'accept-encoding': 'gzip',
            'user-agent': 'okhttp/4.5.0'
        }

        data = {
            'device': '1',
            'password': self.userInfo['phone'] + '@Rumix',
            'download_channel': 'xiaomi',
            'deviceToken': 'AtDW9JY9vcCKiTXf-pWKrgEogrWEo7ATTH7VLzXGKNvE',
            'mobile': self.userInfo['phone'],
            'professions_id': '138',
            'city_id': '195',
            'layer_id': '1',
            'users_id': '0',
            'school_id': '2',
            'province_id': '19'
        }

        url = 'https://www.biguotk.com/api/user/login'

        response = requests.post(url, headers=headers, data=data).json()
        if response['result_code'] == 1:
            self.token = response['data']['token']
            self.userInfo['id'] = str(response['data']['user_id'])
            self.print_('账号:', self.userInfo['phone'], '登录成功')
        else:
            self.print_('账号:', self.userInfo['phone'], '登录失败')

    # 获取用户信息
    def get_user_info(self):
        headers = {
            'Host': 'www.biguotk.com',
            'Devicemodel': 'microsoft',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x6309091b) XWEB/8555',
            'Content-Type': 'application/json',
            'Xweb_xhr': '1',
            'Device': '3',
            'Deviceversion': 'Windows 11 x64',
            'Token': self.token,
            'Appversion': '3.7.1',
            'Accept': '*/*',
            'Sec-Fetch-Site': 'cross-site',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': 'https://servicewechat.com/wx48d6b96427c8a452/85/page-frame.html',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9'
        }

        response = requests.get('https://www.biguotk.com/api/v4/user/info_base?users_id=' + self.userInfo['id'],
                                headers=headers).json()

        if response['result_code'] == 1:
            data = response['data']
            # self.userInfo['phone'] = data['mobile']
            self.userInfo['professions_id'] = data['professions_id']
            self.userInfo['city_id'] = data['city_id']
            self.userInfo['layer_id'] = data['layer']
            self.userInfo['school_id'] = data['school_id']
            self.userInfo['province_id'] = data['province_id']
            self.userInfo['free_fruit_coin'] = data['free_fruit_coin']
            self.userInfo['integral'] = data['integral']
            self.print_('账号:', self.userInfo['phone'], ',获取用户信息成功,当前积分：', self.userInfo['integral'],
                  '当前果币：',
                  self.userInfo['free_fruit_coin'])
        else:
            self.print_('获取用户信息失败')

    # 每日发圈打卡
    def send_daka(self):
        headers = {
            'Host': 'www.biguotk.com',
            'Content-Length': '92',
            'Devicemodel': 'microsoft',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x6309091b) XWEB/8555',
            'Content-Type': 'application/json',
            'Xweb_xhr': '1',
            'Device': '3',
            'Deviceversion': 'Windows 11 x64',
            'Token': self.token,
            'Appversion': '3.7.1',
            'Accept': '*/*',
            'Sec-Fetch-Site': 'cross-site',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': 'https://servicewechat.com/wx48d6b96427c8a452/85/page-frame.html',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9'
        }

        data = {
            "post_table_id": 1,
            "post_table_str": "",
            "content": "今天还要加油鸭!",
            "users_id": self.userInfo['id']
        }

        response = requests.post('https://www.biguotk.com/api/user/posts_v2', headers=headers,
                                 data=json.dumps(data)).json()
        if response['result_code'] == 1:
            self.print_('账号:', self.userInfo['phone'], '今日发圈成功')
        else:
            self.print_('账号:', self.userInfo['phone'], '今日发圈失败')

    # 签到
    def sign_in(self):
        headers = {
            "Host": "www.biguotk.com",
            "device": "1",
            "channel": "xiaomi",
            "devicemodel": "22041211AC",
            "deviceversion": "7.1.2",
            "appversion": "6.5.21",
            "token": self.token,
            "content-type": "application/x-www-form-urlencoded",
            "content-length": "85",
            "accept-encoding": "gzip",
            "user-agent": "okhttp/4.5.0"
        }
        data = {
            "professions_id": self.userInfo['professions_id'],
            "city_id": self.userInfo['city_id'],
            "layer_id": self.userInfo['layer_id'],
            "users_id": self.userInfo['id'],
            "school_id": self.userInfo['school_id'],
            "province_id": self.userInfo['province_id']
        }
        response = requests.post("https://www.biguotk.com/api/v4/task/sign_in", headers=headers, data=data).json()
        # {'result_code': 0, 'result_info': '今天已经打过卡了'}
        if response['result_code'] == 1:
            self.print_('账号:', self.userInfo['phone'], '签到成功')
        else:
            self.print_('账号:', self.userInfo['phone'], response['result_info'])

    # 执行转盘抽奖
    def draw(self):
        url = "https://www.biguotk.com/api/v4/active/draw"
        headers = {
            "Host": "www.biguotk.com",
            "content-length": "45",
            "pragma": "no-cache",
            "cache-control": "no-cache",
            "devicemodel": "22041211AC",
            "user-agent": "Mozilla/5.0 (Linux; Android 7.1.2; 22041211AC Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/81.0.4044.117 Mobile Safari/537.36",
            "content-type": "application/json",
            "accept": "application/json, text/plain, */*",
            "device": "1",
            "deviceversion": "7.1.2",
            "channel": "xiaomi",
            "token": self.token,
            "appversion": "6.5.21",
            "origin": "https://h5.biguotk.com",
            "x-requested-with": "com.dep.biguo",
            "sec-fetch-site": "same-site",
            "sec-fetch-mode": "cors",
            "sec-fetch-dest": "empty",
            "referer": "https://h5.biguotk.com/",
            "accept-encoding": "gzip, deflate",
            "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
        }
        data = {
            "users_id": self.userInfo['id'],
            "type": "day_free_draw"
        }
        response = requests.post(url, headers=headers, data=json.dumps(data)).json()
        if response['result_code'] == 1:
            self.print_('账号:', self.userInfo['phone'], '抽奖成功')
        else:
            self.print_('账号:', self.userInfo['phone'], response['result_info'])

    # 获取可领取积分
    def get_reward(self):
        headers = {
            'Host': 'www.biguotk.com',
            'device': '1',
            'channel': 'xiaomi',
            'devicemodel': '22041211AC',
            'deviceversion': '7.1.2',
            'appversion': '6.5.21',
            'token': self.token,
            'Accept-Encoding': 'gzip',
            'User-Agent': 'okhttp/4.5.0'
        }

        params = {
            'professions_id': self.userInfo['professions_id'],
            'city_id': self.userInfo['city_id'],
            'layer_id': self.userInfo['layer_id'],
            'users_id': self.userInfo['id'],
            'school_id': self.userInfo['school_id'],
            'province_id': self.userInfo['province_id']
        }

        url = 'https://www.biguotk.com/api/v4/task/home'

        response = requests.get(url, headers=headers, params=params).json()
        if response['result_code'] == 1:
            for data in response['data']['daily_tasks']:
                if len(data['prize_ids']) > 0:
                    self.task_prizes(data['name'], data['prize_ids'][0])
        else:
            self.print_('获取可领取积分失败')

    # 查看某一题答案，当作已做题
    def post_answer(self):
        headers = {
            'Host': 'www.biguotk.com',
            'device': '1',
            'channel': 'xiaomi',
            'devicemodel': '22041211AC',
            'deviceversion': '7.1.2',
            'appversion': '6.5.21',
            'token': self.token,
            'Accept-Encoding': 'gzip',
            'User-Agent': 'okhttp/4.5.0'
        }

        params = {
            'exams_id': '76373',
            'mainType': '1',
            'offset': '0',
            'professions_id': self.userInfo['professions_id'],
            'city_id': self.userInfo['city_id'],
            'layer_id': self.userInfo['layer_id'],
            'users_id': self.userInfo['id'],
            'school_id': self.userInfo['school_id'],
            'province_id': self.userInfo['province_id']
        }

        url = 'https://www.biguotk.com/api/v2parse_show'

        response = requests.get(url, headers=headers, params=params).json()
        if response['result_code'] == 1:
            self.print_('账号:', self.userInfo['phone'], '已做题')
        else:
            self.print_('账号:', self.userInfo['phone'], '做题失败')

    # 领取任务奖励
    def task_prizes(self, prize_name, prize_ids):
        url = "https://www.biguotk.com/api/v4/active/take_prizes"
        headers = {
            "Host": "www.biguotk.com",
            "device": "1",
            "channel": "xiaomi",
            "devicemodel": "22041211AC",
            "deviceversion": "7.1.2",
            "appversion": "6.5.21",
            "token": self.token,
            "Content-Type": "application/x-www-form-urlencoded",
            "Content-Length": "104",
            "Accept-Encoding": "gzip",
            "User-Agent": "okhttp/4.5.0",
        }

        data = {
            "prize_ids": prize_ids,
            "professions_id": self.userInfo['professions_id'],
            "city_id": self.userInfo['city_id'],
            "layer_id": self.userInfo['layer_id'],
            "users_id": self.userInfo['id'],
            "school_id": self.userInfo['school_id'],
            "province_id": self.userInfo['province_id']
        }

        response = requests.post(url, headers=headers, data=data).json()
        if response['result_code'] == 1:
            self.print_('账号:', self.userInfo['phone'], '领取', prize_name, '成功')
        else:
            self.print_('账号:', self.userInfo['phone'], '领取', prize_name, '失败')

    def do_task(self):
        # 登录
        self.login()
        # 获取用户信息
        self.get_user_info()
        # 转轮盘
        self.draw()
        # 签到打卡
        self.sign_in()
        # 发圈打卡
        self.send_daka()
        # 提交查看答案
        self.post_answer()
        # 领取积分
        task.get_reward()
        # 获取用户信息
        self.get_user_info()
        pass


zkbgPhone = '13413749836'
if 'zkbgPhone' in os.environ:
    zkbgPhone = os.environ.get("zkbgPhone")
    print('获取到环境变量zkbgPhone=' + zkbgPhone)
if __name__ == '__main__':
    # 多个用@分隔
    list = zkbgPhone.split('@')
    print_list = []
    for i in range(len(list)):
        task = ZkbgTask('自考笔果', list[i])
        print_list.extend(task.run())
    send("自考笔果", print_list)
